package com.prp.db.daoimpl.mysql;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.prp.db.idao.IMailboxDAO;
import com.prp.db.po.Mailbox;

/**
 * A data access object (DAO) providing persistence and search support for
 * Mailbox entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.prp.db.daoimpl.mysql.Mailbox
 * @author MyEclipse Persistence Tools
 */

public class MailboxDAO extends BaseHibernateDAO implements IMailboxDAO{
	private static final Log log = LogFactory.getLog(MailboxDAO.class);
	// property constants
	public static final String MAIL_ID = "mailId";
	public static final String SENDER_ID = "senderId";
	public static final String RECEIVER_ID = "receiverId";
	public static final String STATE = "state";
	public static final String TITLE = "title";

	public void save(Mailbox transientInstance) {
		log.debug("saving Mailbox instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Mailbox persistentInstance) {
		log.debug("deleting Mailbox instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Mailbox findById(java.lang.Integer id) {
		log.debug("getting Mailbox instance with id: " + id);
		try {
			Mailbox instance = (Mailbox) getSession().get(
					"com.prp.db.po.Mailbox", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Mailbox instance) {
		log.debug("finding Mailbox instance by example");
		try {
			List results = getSession().createCriteria(
					"com.prp.db.po.Mailbox").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Mailbox instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Mailbox as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List findBySenderReceiver(Integer senderId, Integer receiverId){
		log.debug("finding Mailbox instance with sender_id and receiver_id");
		try {
			String queryString = "from Mailbox as model where model."
					+ SENDER_ID + "= ? and "+RECEIVER_ID+"= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, senderId);
			queryObject.setParameter(1, receiverId);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByMailId(Object mailId) {
		return findByProperty(MAIL_ID, mailId);
	}

	public List findBySenderId(Object senderId) {
		return findByProperty(SENDER_ID, senderId);
	}

	public List findByReceiverId(Object receiverId) {
		return findByProperty(RECEIVER_ID, receiverId);
	}
	
	public List findBySenderReceiver(Object senderId, Object receiverId){
		return null;
	}
	
	public List findByState(Object state) {
		return findByProperty(STATE, state);
	}

	public List findByTitle(Object title) {
		return findByProperty(TITLE, title);
	}

	public List findAll() {
		log.debug("finding all Mailbox instances");
		try {
			String queryString = "from Mailbox";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Mailbox merge(Mailbox detachedInstance) {
		log.debug("merging Mailbox instance");
		try {
			Mailbox result = (Mailbox) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Mailbox instance) {
		log.debug("attaching dirty Mailbox instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Mailbox instance) {
		log.debug("attaching clean Mailbox instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}